package com.grts.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.grts.pojo.MajorBean;
import com.grts.pojo.Teacher;
import com.grts.service.IMajorService;
import com.grts.service.Teacherservice;
import com.grts.utils.BaseAction;
import com.grts.utils.PageModel;

@Controller
@RequestMapping("/teacher")
public class TeacherControllor{
	@Resource
	private Teacherservice ts;
	@Resource
	private IMajorService ms;
	@RequestMapping("/query")
	public String queryByPage(Teacher teacher,Map m,@RequestParam(name="pageSize",required=false,defaultValue="3")int pageSize,
								@RequestParam(name="currentPage",required=false,defaultValue="1")int currentPage){

		
		Teacher t=new Teacher();
		t.setTname(teacher.getTname());
		t.setCurrentPage((currentPage-1)*pageSize);
		t.setPageSize(pageSize);
		PageModel pageModel=ts.queryPageModel(t,currentPage,pageSize);
		
		m.put("teacher", teacher);
		m.put("pageModel", pageModel);
		return "../teacher/teacher";
	
}

	@RequestMapping("/init")
	public String init(Map m){
		List<MajorBean> list=ms.queryAll();
		m.put("list", list);
		return "../teacher/toaddOrupdate";
	}
	@RequestMapping("/addOrupdate")
	public String addOrupdate(Teacher t){
		if(t.getTid()!=null){
			System.out.println(t);
			ts.updateByPrimaryKey(t);
		}else{
			t.setState(0);
			ts.insert(t);
		}
		return "redirect:query";
	}
	@RequestMapping("/queryById")
	public String queryById(Map m,@RequestParam(name="tid",required=false,defaultValue="1")int id){
		Teacher t=ts.selectByPrimaryKey(id);
		m.put("t", t);
		List<MajorBean> list=ms.queryAll();
		m.put("list", list);
		return "../teacher/toaddOrupdate";
	}
	@RequestMapping("/delete")
	public String delete(@RequestParam(name="tid",required=false,defaultValue="1")int id){
		ts.deleteByPrimaryKey(id);
		return "redirect:query";
	}
}
